<template>
  <div class="list" ref="wrapper">
    <div class="content">
      <div class="area">
        <div class="title border-topbottom">当前城市</div>
        <div class="button-list">
          <div class="button-wrapper">
            <div class="button">{{this.city}}</div>
          </div>
        </div>
      </div>
      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list">
          <div
            class="button-wrapper"
            v-for="item of hotCities"
            :key="item.id"
            @click="handleClick(item.name)"
          >
            <div class="button">{{item.name}}</div>
          </div>
        </div>
      </div>
      <div class="area" v-for="(item,key) of cities" :ref="key" :key="key">
        <div class="title border-topbottom">{{key}}</div>
        <div class="item-list">
          <div
            class="item border-bottom"
            v-for="i of item"
            :key="i.id"
            @click="handleClick(i.name)"
          >{{i.name}}</div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import BetterScroll from "better-scroll";
import { mapState, mapMutations } from "vuex";
export default {
  props: {
    cities: Object,
    hotCities: Array,
    alphabet: String
  },
  computed: {
    ...mapState(["city"])
  },
  watch: {
    alphabet() {
      if (this.alphabet) {
        const ele = this.$refs[this.alphabet][0];
        this.scroll.scrollToElement(ele);
      }
    }
  },

  mounted() {
  },
  activated(){
    this.scroll = new BetterScroll(this.$refs.wrapper);
    console.log(this.scroll)
  },
  data() {
    return {
      scroll: null,
      timer:null
    };
  },
  methods: {
    ...mapMutations(["changeCity"]),
    handleClick(city) {
      // this.$store.dispatch('changeCity',city)
      // this.$store.commit('changeCity',city)
      this.changeCity(city);
      this.$router.push("/");
    }
  }
}
</script>
<style lang="less" scoped>
.list {
  /* 不使用浏览器自带的滚动条 所以overflow hidden */
  overflow: hidden;
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  top: 1.78rem;
  .border-topbottom {
    &:before {
      border-color: #ccc;
    }
    &:after {
      border-color: #ccc;
    }
  }
  .border-bottom {
    border-color: #ccc;
  }
  .title {
    line-height: 0.44rem;
    background-color: #eee;
    padding-left: 0.2rem;
    color: #666;
    font-size: 0.26rem;
  }
  .button-list {
    /*触发bfc清除浮动*/
    overflow: hidden;
    // display: inline-block;
    padding: 0.1rem 0.6rem 0.1rem 0.1rem;
    .button-wrapper {
      width: 33.33%;
      float: left;
      .button {
        margin: 0.1rem;
        padding: 0.1rem;
        text-align: center;
        border: 0.02rem solid #ccc;
        border-radius: 0.06rem;
      }
    }
  }
  .item-list {
    .item {
      line-height: 0.76rem;
      padding-left: 0.2rem;
    }
  }
}
</style>
